/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jl.tools.stkeditor.manager;

/**
 * 
 * @author LuHongjun
 */
public final class DataPosLen {

	private StringBuilder pos;
	private int curPos;
	private StringBuilder len;
	private StringBuilder data;
	private final String dataStar = "private static final byte[] DATA = new byte[]{\n";

	private final String posStar = "";
	private final String lenStar = "";

	private final String endData = "};\n\n";

	private final String preOffset = "    public final static short OFF_";
	private final String preLength = "    public final static short LEN_";

	public DataPosLen() {
		init();
	}

	private void init() {
		pos = new StringBuilder();
		len = new StringBuilder();
		data = new StringBuilder();
		curPos = 0;
		data.append(dataStar);
		pos.append(posStar);
		len.append(lenStar);
	}

	public void add(String data, int len, int index, String name) {
		String temp = name.trim().equals("") ? index + "" : name.trim();
		String lenStr = preLength + temp + " = (short)" + len + ";\n";
		String posStr = preOffset + temp + " = (short)" + curPos
				+ ";\n";
		curPos += len;
		
		this.data.append("    //index : ").append(index).append("\n");
		this.data.append(data);
		this.pos.append(posStr);
		this.len.append(lenStr);
	}

	public String getFinalData() {
		data.append(endData);
		return data.toString();
	}

	public String getFinalOffsetAndLength() {
		StringBuilder sb = new StringBuilder();
		sb.append("public class L{\n").append(pos).append("\n").append(len)
				.append("}");
		return sb.toString();
	}
}
